package com.wang.extension;

import com.wang.domain.User;
import com.wang.domain.UserProxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Bean初始化扩展点
 * spring容器中每个bean初始化都会调用一次
 * 可以做代理用，返回代理对象
 *
 * @Author wly
 * @Date 2018/7/11 10:23
 */
@Component
public class TestBeanPostProcessor implements BeanPostProcessor {

    /**
     * Bean创建之后，调用初始化方法之前执行
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("TestBeanPostProcessor.postProcessBeforeInitialization==========" + bean.getClass());
        if (bean instanceof User) {
            return new UserProxy();
        }
        return bean;
    }

    /**
     * Bean创建之后，调用初始化方法之后执行
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("TestBeanPostProcessor.postProcessAfterInitialization==========" + bean.getClass());
        return bean;
    }
}
